package com.stox.dto.web;

import java.lang.reflect.InvocationTargetException;

import lombok.Data;

@Data
public class BeanPropertyDescriptor {

	private String name;
	
	private Class<?> type;
	
	private Object value;
	
	private BeanDescriptor beanDescriptor;
	
	public Object getValue(){
		if(null != beanDescriptor && null != beanDescriptor.getBean()){
			Object bean = beanDescriptor.getBean();
			try {
				value = bean.getClass().getMethod("get"+getName(), new Class<?>[0]).invoke(bean, new Object[0]);
			} catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException
					| NoSuchMethodException | SecurityException e) {
				throw new RuntimeException(e);
			}
		}
		return value;
	}

	public void setValue(Object value){
		this.value = value;
		if(null != beanDescriptor && null != beanDescriptor.getBean()){
			Object bean = beanDescriptor.getBean();
			try { 
				bean.getClass().getMethod("set"+getName(), getType()).invoke(bean, value);
			} catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException
					| NoSuchMethodException | SecurityException e) {
				throw new RuntimeException(e);
			}
		}
	}
	
}
